package com.abo.yaas.view.widgets.tables;

import java.io.ByteArrayInputStream;
import java.io.InputStream;

import com.abo.yaas.domain.Auction;
import com.abo.yaas.domain.AuctionState;
import com.abo.yaas.view.DynamicImageResource;
import com.abo.yaas.view.ViewHandler;
import com.abo.yaas.view.widgets.AuctionView;
import com.abo.yaas.webservice.ResourceProvider;
import com.abo.yaas.webservice.client.EntityProviderProxy;
import com.vaadin.addon.jpacontainer.JPAContainer;
import com.vaadin.addon.jpacontainer.filter.Filters;
import com.vaadin.data.Item;
import com.vaadin.event.ItemClickEvent;
import com.vaadin.event.ItemClickEvent.ItemClickListener;
import com.vaadin.terminal.StreamResource;
import com.vaadin.terminal.StreamResource.StreamSource;
import com.vaadin.ui.Component;
import com.vaadin.ui.Embedded;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Table;

@SuppressWarnings("serial")
public class AuctionTable extends YaasPagedTable {
    private JPAContainer<Auction> container;

    private com.vaadin.addon.jpacontainer.Filter filter = null;

    private final String[] columns = new String[] { "preview", "title",
            "startTime", "ownerUserName", "category" };

    private final ColumnGenerator imageColumnGenerator = new ColumnGenerator() {
        @Override
        public Component generateCell(final Table source, final Object itemId,
                final Object columnId) {
            final StreamSource streamSource = new StreamSource() {
                @Override
                public InputStream getStream() {
                    final Item item = source.getItem(itemId);
                    final byte[] bas = (byte[]) item.getItemProperty("preview")
                            .getValue();
                    if (bas != null) {
                        return new ByteArrayInputStream(bas);
                    }
                    return null;
                }
            };
            final StreamResource resource = new DynamicImageResource(
                    streamSource, getApplication());
            final Embedded embedded = new Embedded("", resource);
            embedded.setWidth("100px");
            return embedded;
        }
    };

    public AuctionTable() {
        setupContainer();
        setupLayout();
        addListener(new ItemClickListener() {
            @Override
            public void itemClick(ItemClickEvent event) {
                if (event.isDoubleClick()) {
                    final Item item = event.getItem();
                    final Object id = item.getItemProperty("id").getValue();
                    ViewHandler.activateView(AuctionView.class,
                            ResourceProvider.getEntityProvider(Auction.class)
                                    .getEntity(id));
                }
            }
        });
    }

    @Override
    public HorizontalLayout createControls() {
        final HorizontalLayout layout = super.createControls();
        layout.setSizeUndefined();
        return layout;
    }

    public void filter(String valueOf) {
        if (filter != null) {
            container.removeFilter(filter);
        }
        filter = Filters.like("title", valueOf, false);
        container.addFilter(filter);
        refreshCurrentPage();
    }

    private void setupContainer() {
        final EntityProviderProxy<Auction> provider = ResourceProvider
                .getEntityProvider(Auction.class);
        final com.vaadin.addon.jpacontainer.Filter filter = Filters.eq("state",
                AuctionState.AS_ACTIVE);
        container = new JPAContainer<Auction>(Auction.class);
        container.addFilter(filter);
        container.setEntityProvider(provider);
        container.setApplyFiltersImmediately(true);
        container.applyFilters();
        setContainerDataSource(container);
    }

    private void setupLayout() {
        setSizeFull();
        setVisibleColumns(new String[] { "preview", "title", "startTime",
                "ownerUserName" });
        addGeneratedColumn("preview", imageColumnGenerator);
        // final String[] columns = new String[]
        // { "preview", "title", "startTime", "ownerUserName", "category" };
        // setColumnExpandRatio("preview", 0.1f);
        setColumnExpandRatio("title", 0.6f);
        /*
         * setColumnExpandRatio("startTime", 0.2f);
         * setColumnExpandRatio("ownerUserName", 0.2f);
         * setColumnExpandRatio("category", 0.1f);
         */

    }
}
